/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package view;

import controller.cSquare;
import controller.cCheckerboard;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import javax.swing.JFrame;
import model.Checkerboard;

/**
 * @author Robert
 * @desc: On utilise un panel principal (grid) contenant autant de panels (pane) qu'il y'a de cases dans le damier.
 *
 */
public class Grid extends JPanel 
{
    private int gridSize = 500;

    public Grid(cCheckerboard cb, JFrame mainFrame, UserOption uOption){
         int nbColumn = 10, nbRow = 10;
         JPanel grid = new JPanel (new GridLayout(nbRow, nbColumn));
        
        /* Create and color all square */
        for(int j=0; j<nbRow; j++)
        {
            for(int i=0; i< nbColumn; i++)
            {           
               cSquare square = new cSquare(i, j, (gridSize/nbRow), grid, cb, uOption);    /* Case du damier qui contiendra les listeners */
               Graphics canvas = null;
                if(cb.getCheckerboard().getSquare(i, j).getColor() == 'L')
                {
                    square.setBackground(Color.white);
                    square.removeListerner(true);
                }
                else if(cb.getCheckerboard().getSquare(i, j).getColor() == 'D')
                {
                    square.setBackground(Color.blue);
                    
                    /* Placement des pions */
                    if(cb.getCheckerboard().getSquare(i, j).getIsOccupied() == Checkerboard.P1_PAWN)
                    {
                        square.setPieceColor(cSquare.P1Color);
                        square.paintComponents(canvas);
                    }
                    else if(cb.getCheckerboard().getSquare(i, j).getIsOccupied() == Checkerboard.P2_PAWN)
                    {
                        square.setPieceColor(cSquare.P2Color);
                        square.paintComponents(canvas);
                    }
                }
               grid.add(square);
            }
        }
        grid.setSize(gridSize, gridSize);
        mainFrame.add(grid);
        mainFrame.setVisible(true);

    }


}

